package edu.ahut.corejava3;

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

public class CountWordsWithMap {
    public static void main(String[] args) {
        String str = " To     know  everything is##    <to> know nothing.  ";
        String[] words = str.trim().toLowerCase().split("\\W+");
        System.out.println(Arrays.asList(words));//对象数组列表输出

        Map<String, Integer> map = new TreeMap<String, Integer>();//表格初始化

        for (String word : words) {
            if (!map.containsKey(word)) {//是第一次出现
                map.put(word, 1);
            } else {
                Integer old = map.get(word);
                map.put(word, old + 1);
            }
        }

        System.out.println(map);

        System.out.println(words.length);//总的单词数
        for (String word : map.keySet()) {//遍历map
            System.out.println(word + ":" + map.get(word));
        }

    }
}
